#include<stdio.h>
#include<stdlib.h>
#include<string.h>

////////////////////////////////////////////////////////////////////////////////


struct list{
	int h;
	struct list *nxt;
	};
struct list *start;

////////////////////////////////////////////////////////////////////////////////

void print()
{
	struct list *r;
	printf("[ ");
	for(r=start;r!=NULL;r=r->nxt)
		{printf("%d",r->h);
		printf(",");}
	printf(" ]");
}
////////////////////////////////////////////////////////////////////////////////


insertion(int h,int k)
{    int a;
     struct list *AA,*ELEM;
     AA=(struct list*)malloc(sizeof(struct list));
     if(AA==NULL){return;}
	     AA->h=h;
     if(k==1)
	{AA->nxt=start; start=AA; return;}
     for(a=k-2,ELEM=start;ELEM!=NULL&&a>=1;ELEM=ELEM->nxt,a--);
     if(a>=1)
            {printf("\nError Encountered.");
                                   return;}
     AA->nxt=ELEM->nxt;
     ELEM->nxt=AA;
     printf("\n %d number inserted at %d position.", h, k);
     
     
}

////////////////////////////////////////////////////////////////////////////////


void insert(m)
{
	struct list *newl;
	newl=(struct list *)malloc(sizeof(struct list));
	if(newl==NULL)
	{	printf("Error!");
		return;
	}
	if(start==NULL){

		newl->h=m;
		start=newl;
		start->nxt=NULL;}
	else{	
		newl->h=m;
		struct list *kun;
		for(kun=start;kun->nxt!=NULL;kun=kun->nxt);
		kun->nxt=newl;
		newl->nxt=NULL;}
}


////////////////////////////////////////////////////////////////////////////////

remov(num)
{

struct list *ELEM=start;
     if(ELEM->h==num)
     {start=start->nxt;
	return;}

     for(;ELEM->nxt!=NULL;ELEM=ELEM->nxt)
	{if(ELEM->nxt->h==num)
		ELEM->nxt=ELEM->nxt->nxt;}}

///////////////////////////////////////////////////////////////////////////////*/

void rev(){
	struct list *strt,*kun,*flag;
	int tempo;	
	strt=start;
	for(kun=start;kun->nxt!=NULL;kun=kun->nxt);
	while(kun!=strt&&kun->nxt!=strt)
	{
		tempo=strt->h;
		strt->h=kun->h;
		kun->h=tempo;
		strt=strt->nxt;
		flag=kun;
		for(kun=start;kun->nxt!=flag&&kun!=NULL;kun=kun->nxt);

	}
print();}

////////////////////////////////////////////////////////////////////////////////


void sort()
{    int temp,kun;
     struct list *st,*sr;
     st=start;
     for(;st!=NULL;st=st->nxt){
	kun=st->h;
	for(sr=start;sr!=NULL;sr=sr->nxt)
		if(kun < sr->h)
			{temp=sr->h;
			sr->h=st->h;
			st->h=temp;}}
     print();
}

///////////////////////////////////////////////////////////////////////////////

ex()
{
int ii,kk;
printf("Enter the size of list : ");
scanf("%d", &kk);
for(ii=0; ii<kk; ii++)
{append();}

}

///////////////////////////////////////////////////////////////////////////////*/

inde()
{
	int number;
	printf("Enter the number of which you want to calculate the index of: ");
	scanf("%d", &number);
	struct list *ELEM;
	ELEM=start;
	int o=1;
	for(;ELEM!=NULL;ELEM=ELEM->nxt,o++)
                                   if(ELEM->h==number)
                                   { printf("\n%d Position!",o); return;}
	if(ELEM==NULL)
	printf("\ninvalid number");}

////////////////////////////////////////////////////////////////////////////////

append(){
	int k;
	printf("Enter the number to be appended");
	scanf("%d", &k);
	insert(k);
	}

////////////////////////////////////////////////////////////////////////

void count(nu)
{
     int i=0;
     struct list *ELEM;
     ELEM=start;
     for(;ELEM!=NULL;ELEM=ELEM->nxt)
                                  if(ELEM->h==nu)
                                                i++;
     printf("\n%d APPEARS %d TIMES",nu, i);}



main()
{
char var[10], str[10];

	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
	printf("\nDO NOT IGNORE WHITE SPACES\n\nWrite the commands AS GIVEN in the examples\n\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
	printf("\n\n// Enter the list e.g. list_name = [ d1,d2,d3,d4,d5 ]\n\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
	printf("\n\n>>> ");
	scanf ("%s = [ %s ]", var, str);


  char *p;
  int i;
  p = strtok (str,", ");
  while (p != NULL)
  {
    int i = atoi(p);
    insert(i);
print();
    printf("\n");
    p = strtok (NULL, ", ");
}

for(;;){

	printf("\n>>> ");

	char d[15];
	int in;
	scanf("%s", d);
		if ((strcmp(d, "append")) == 0)		append(in);
		if ((strcmp(d, "print")) == 0)		print();
		if ((strcmp(d, "reverse")) == 0)	rev();
		if ((strcmp(d, "index")) == 0)		inde();
		if ((strcmp(d, "sort")) == 0)		sort();
		if ((strcmp(d, "exit")) == 0)		exit(1);	
		if ((strcmp(d, "del")) == 0)		
				{int num;
				scanf("%d", &num);
				remov(num);}	
		if ((strcmp(d, "insert")) == 0)		
				{int kn,wh;
				scanf("%d", &kn);
				scanf("%d", &wh);
				insertion(kn, wh);}	
		if ((strcmp(d, "count")) == 0)
				{int nu;
				scanf("%d", &nu);
				count(nu);}	

		if ((strcmp(d, "extend")) == 0)		ex();

}}
